﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using log4net;

namespace Proxy
{
    class Program
    {
        static void Main(string[] args)
        {
            WebRequest.DefaultWebProxy = null;
            if (args.Length < 1)
            {
                Console.WriteLine("usage: Proxy.exe <listening_port>");
            }
            else
            {
                string mode = ConfigurationManager.AppSettings["mode"];
                int maxRequests = int.Parse(ConfigurationManager.AppSettings["maxRequests"]);
                int timeLimit = int.Parse(ConfigurationManager.AppSettings["timelimit"]);
                Logger log = new Logger();
                ILog logger = log.getLogger();
                BlackWhiteListManager manager = new BlackWhiteListManager(maxRequests, timeLimit);
                manager.addToBlackList("http://www.walla.co.il/");
                manager.addToBlackList("http://www.one.co.il/");
                WebClient client = new WebClient();
                string externalIP = "";
                try
                {
                    externalIP = client.DownloadString("http://whatismyip.com/automation/n09230945.asp");
                }
                catch
                {
                }
                HttpListener listener = new HttpListener();
                listener.Prefixes.Add("http://*:" + args[0] + "/");
                listener.Start();
                Console.WriteLine("Listening on port {0}..." ,args[0]);
                while (true)
                {
                    HttpListenerContext context = listener.GetContext();
                    Connection connection = new Connection(context, mode, ref manager, args[0], ref logger, externalIP);
                    new Thread(new ThreadStart(connection.run)).Start();
                }
            }
        }
        static void test(ref BlackWhiteListManager manager)
        {
            string ip1,ip2,ip3,ip4;
            ip1 = "123.123.123.123";
            ip2 = "123.123.123.124";
            ip3 = "123.123.123.125";
            ip4 = "123.123.123.126";

            manager.addToWhiteList(ip1);
            manager.addToWhiteList(ip2);
            manager.addToWhiteList(ip3);
            manager.addToWhiteList(ip4);

            for (int i = 0; i < 50; i++)
            {
                if (manager.exceedsSecurityRules(ip1))
                {
                    Console.WriteLine("exceed");
                    break;
                }
                else
                    manager.addToConnections(ip1);
            }
            Thread.Sleep(5000);
            for (int i = 0; i < 51; i++)
            {
                if (manager.exceedsSecurityRules(ip1))
                {
                    Console.WriteLine("exceed");
                    break;
                }
                else
                    manager.addToConnections(ip1);
            }

            for (int i = 0; i < 49; i++)
            {
                if (manager.exceedsSecurityRules(ip1))
                {
                    Console.WriteLine("exceed");
                    break;
                }
                else
                    manager.addToConnections(ip1);
            }
            while (true)
            {
                if (manager.exceedsSecurityRules(ip1))
                {
                    Console.WriteLine("exceed");
                    break;
                }
                else
                    manager.addToConnections(ip1);
            }
            
        }

    }
}
